package com.byd.streamer;

import java.io.IOException;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.byd.base.BaseActivity;
import com.byd.base.BasePresenter;
import com.byd.presenter.MainPresenter;
import com.byd.utils.CommonUtils;
import com.byd.utils.Lg;
import com.ffmpeg.player.FFmpegPlayer.State;

public class MainActivity extends BaseActivity implements OnClickListener, OnItemClickListener, TextWatcher {
	private MainPresenter mPresenter;
	private EditText mEdtUrl;
	private Button mBtnStart;
	private View mBtnStop;
	
	
	@Override
	protected int onInflateView() {
		return R.layout.activity_main;
	}

	@Override
	protected BasePresenter<? extends BaseActivity> onCreatePresenter() {
		return mPresenter = new MainPresenter();
	}

	@Override
	protected void onCreate() {
		mEdtUrl = (EditText) findViewById(R.id.edt_outputUrl);
		mEdtUrl.setText(MainPresenter.URL);
		mBtnStart = (Button) findViewById(R.id.btn_start);
		mBtnStart.setOnClickListener(this);
		mBtnStop = findViewById(R.id.btn_stop);
		mBtnStop.setOnClickListener(this);
		mBtnStop.setEnabled(false);
		((ListView) findViewById(R.id.lv_videos)).setOnItemClickListener(this);
	}

	public void setAdapter(BaseAdapter adapter) {
		((ListView) findViewById(R.id.lv_videos)).setAdapter(adapter);
	}
	
	@Override
	public void onClick(View v) {
		int mulriple;
		switch (v.getId()) {
		case R.id.btn_start:
			mPresenter.start();
			break;
		case R.id.btn_stop:
			mPresenter.stop();
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		mPresenter.select(position);
	}
	
	public void setState(int state) {
		boolean stop = state == State.STOPED;
		if(state == State.PLAYING) {
			mBtnStart.setText("Pause");
		}
		else {
			mBtnStart.setText("Start");
		}
		mBtnStop.setEnabled(!stop);
		mEdtUrl.setEnabled(stop);
		findViewById(R.id.lv_videos).setEnabled(stop);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		//nothing
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		//nothing
	}

	@Override
	public void afterTextChanged(Editable s) {
		mPresenter.setUri(mEdtUrl.getText().toString());
	}
}
